home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / sep91.zip / 9N09050A < prev    next >
Text File  |  1991-07-17  |  3KB  |  85 lines

  1. Listing 2. Stream sockets example. The client program.
  2.  
  3.  
  4. /* send -- sends strings to a local or remote server in the Internet domain. 
  5.  * "send" operates in conjunction with the "receive" server program.
  6.  *     Usage:  send <server name>
  7.  */ 
  8.  
  9. #include  <sys/types.h>
  10. #include  <sys/socket.h>
  11. #include  <netdb.h>
  12. #include  <netinet/in.h>
  13.  
  14. #define   FAIL   1
  15. #define   SUCC   0
  16. #define   PROMPT  printf("type string to be sent (ctrl-d to exit) : ")
  17. #define   PORT_NUMBER  2227          /* the port number through which client
  18.                                       * server communication takes places. 
  19.                                       * Must be the same in client and server. 
  20.                                       */
  21.  
  22. /* ---------------------------------------------------------------------- */
  23.  
  24. main(ac, av)
  25.     int ac;   char **av ;
  26. {
  27.     int sock ;                       /* client process socket descriptor  */
  28.     struct sockaddr_in sock_addr ;   /* Internet socket address structure */
  29.     char line[256] ;                 /* message to be sent                */    
  30.     char message[12] ;               /* termination message from server   */
  31.     struct hostent *host_struct, *gethostbyname() ;
  32.     char *server_name = av[1] ;      /* remote server name                */ 
  33.     char *progr_name = av[0] ;
  34.     void error() ;                   /* the same routine of listing 1     */
  35.  
  36.  
  37.     if (ac != 2)  {
  38.         fprintf(stderr, "error -- usage: %s <server name>\n", progr_name);
  39.         exit(FAIL) ;
  40.     }
  41.  
  42.     /* get server address */
  43.     if ((host_struct = gethostbyname(server_name)) == NULL)  {
  44.         fprintf(stderr, "%s: unknown server %s\n", progr_name, server_name) ;   
  45.         exit(FAIL) ;
  46.     } 
  47.  
  48.     /* allocate a stream socket descriptor */
  49.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  50.         error(progr_name, ": error opening socket") ;
  51.  
  52.     /* copy server address and type to socket structure */
  53.     bcopy(host_struct->h_addr, (char *)&sock_addr.sin_addr.s_addr,
  54.           host_struct->h_length) ; 
  55.     sock_addr.sin_port = PORT_NUMBER ;
  56.     sock_addr.sin_family = AF_INET ;
  57.  
  58.     /* connect to remote server process */
  59.     if (connect(sock, (struct sockaddr *)&sock_addr, sizeof sock_addr) == -1)  {
  60.         if (close(sock) == -1)
  61.             error("error closing client socket", "") ;
  62.         error(progr_name, ": socket connection error") ;
  63.     }
  64.  
  65.     /* send lines to remote host */
  66.  
  67.     while (PROMPT, gets(line) != NULL)
  68.         if (write(sock, line, sizeof line) < 0)
  69.             error(progr_name, ": error writing to remote host") ;
  70.  
  71.     /* with the following shutdown the client manifests its intention 
  72.      * not to send any more data to the server.
  73.      */
  74.     if (shutdown(sock, 1) == -1)
  75.         error(progr_name, ": shutdown") ;
  76.  
  77.     /* read server termination message */
  78.     if (read(sock, message, sizeof message) != sizeof message)
  79.         error(progr_name, ": read") ; 
  80.     printf("\n%s\n", message) ;
  81.     if (close(sock) == -1)
  82.         error("error closing client socket", "") ;
  83.     exit(SUCC) ;
  84. }
  85.